namespace System.Xml.Serialization.Internal
{
    using System;
    using System.Collections;

    [Serializable]
    internal class XmlFormatterCache
    {
        private readonly Hashtable hashtable = new Hashtable();

        public IXmlTypeFormatter this[IXmlTypeFormatter formatter]
        {
            get
            {
                IXmlRuntimeSerializerInternal serializer = XmlRuntimeHelper.GetSerializer(formatter);
                if (serializer != null)
                {
                    Type type = serializer.RuntimeType;
                    if (type != null)
                    {
                        if (!this.hashtable.ContainsKey(type))
                        {
                            this.hashtable[type] = formatter;
                        }
                    }
                    return (IXmlTypeFormatter)this.hashtable[type];
                }
                return null;
            }
        }

        public IXmlTypeFormatter this[Type type]
        {
            get
            {
                IXmlTypeFormatter formatter = null;
                if (type != null)
                {
                    if (!this.hashtable.ContainsKey(type))
                    {
                        formatter = new XmlTypeSerializer(type);
                        this.hashtable[type] = formatter;
                        return formatter;
                    }
                    return (IXmlTypeFormatter)this.hashtable[type];
                }
                return null;
            }
        }
    }
}

